package com.zoranjoka.chordis.pages.add;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.zoranjoka.chordis.dao.ArtistDAO;
import com.zoranjoka.chordis.entities.Album;
import com.zoranjoka.chordis.entities.AlbumBean;
import com.zoranjoka.chordis.entities.Artist;
import com.zoranjoka.chordis.pages.edit.EditArtist;

public class AddAlbum {

	@Inject 
	private ArtistDAO dataArtist;
	
	@InjectPage
	private EditArtist page;
	
	@Property 
	@Persist
	private long id;
	@Property
	private String name;
	@Property
	private int year;
	@Property
	private Artist artist;
	@Property 
	private Album album;
	
	void onActivate(long id){
		System.out.println("Activated:"+id);
		this.id=id;

//		album = new AlbumBean();
//		album.setName("jelen");
//		album.setYear(100200);
//		album.setArtist(artist);
//		artist.addAlbum(album);
//		dataArtist.update(artist);
//		artist = null;
//		artist = (ArtistBean) dataArtist.findById(id);
//		album.setName(name);
//		album.setYear(year);
//		album.setArtist(artist);
//		artist.addAlbum(album);
//		dataArtist.save(artist);
//		album=null;
	}
	
	@OnEvent(component="newAlbumForm")
	@CommitAfter
	Object onSaveAlbum()
	{
		System.out.println("Activated save:"+id);
		artist = dataArtist.findById(id);
		album = new AlbumBean();
		album.setName(name);
		album.setYear(year);
		artist.addAlbum(album);
		album.setArtist(artist);
//
		dataArtist.update(artist);
		return com.zoranjoka.chordis.pages.Album.class;
	}
}
